package com.api.wall.getinbox;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IWallApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.InboxEventApi;
import com.zyncro.api.model.InboxEventApiType;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(locations = "classpath:/wall/wall-inbox.xml")
public class GetInbox extends AbstractApiTest {

	@Value("#{user_toGet.appId}")
	private String userUrn;

	@Value("#{user_toGet.password}")
	private String userPassword;

	@Value("#{user_toGet.totalInbox}")
	private int totalInbox;

	@Value("#{user_toGet.totalInbox}")
	private int totalComments;

	@Value("#{user_toGet.totalInvitations}")
	private int totalInvitations;

	@Value("#{user_toGet.totalFollows}")
	private int totalFollows;

	@Value("#{user_toGet.totalLikes}")
	private int totalLikes;

	@BeforeClass
	protected void setParameters() throws Exception {

		getApi().login(userUrn, userPassword);
	}

	@Test
	public void testGetInboxAll() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = null;
		boolean onlyUnread = false;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalInbox, "Total inbox notifications");
	}

	@Test
	public void testGetInboxComments() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = new ArrayList<InboxEventApiType>();
		inboxTypes.add(InboxEventApiType.COMMENT);

		boolean onlyUnread = false;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalComments, "Total comment notifications");
	}

	@Test
	public void testGetInboxInvitation() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = new ArrayList<InboxEventApiType>();
		inboxTypes.add(InboxEventApiType.INVITATION);

		boolean onlyUnread = false;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalInvitations, "Total invitation notifications");
	}

	@Test
	public void testGetInboxFollows() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = new ArrayList<InboxEventApiType>();
		inboxTypes.add(InboxEventApiType.FOLLOW);

		boolean onlyUnread = false;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalFollows, "Total follow notifications");
	}

	@Test
	public void testGetInboxLikes() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = new ArrayList<InboxEventApiType>();
		inboxTypes.add(InboxEventApiType.LIKE);

		boolean onlyUnread = false;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalLikes, "Total like notifications");
	}

	@Test
	public void testGetInboxOnlyUnread() throws ZyncroApiException {

		List<InboxEventApiType> inboxTypes = null;
		boolean onlyUnread = true;

		String date = null;
		int itemsCount = 0;

		PagedItemsResultApi<InboxEventApi> result = getApi().getService(IWallApiService.class)
				.getInbox(date, itemsCount, onlyUnread, inboxTypes);

		Assert.assertEquals(result.getTotalItems(), totalInbox - totalInvitations, "Total unread inbox notifications");
	}
}
